package com.itqn.servlet;

import com.itqn.service.CategoryService;
import com.itqn.service.Impl.CategoryServiceImpl;
import com.itqn.service.Impl.ProductServiceImpl;
import com.itqn.service.ProductService;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(value = "/CategoryDeleteServlet")
public class CategoryDeleteServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @SneakyThrows
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数
        String id = request.getParameter("id");
        //创建业务逻辑层对象
        CategoryService categoryService=new CategoryServiceImpl();
        // 创建product业务逻辑层对象
        ProductService productService=new ProductServiceImpl();
        Integer count = productService.count(Long.valueOf(id));

        if(count>0){
            PrintWriter writer = response.getWriter();
            writer.write(count>0?"on":"ok");
        }else {
            //调用删除的方法
            int result = categoryService.deleteCategory(Integer.parseInt(id));
            PrintWriter writer = response.getWriter();
            writer.write(result>0?"ok":"no");
        }

    }
}